package com.sys.basics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import com.sys.basics.entity.SysOrg;
import com.sys.basics.dao.SysOrgDao;
import com.sys.basics.service.SysOrgService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;


/**
 * @author: ares
 * @date: 2021-10-08
 * @description: 系统组织表 服务实现类
 */
@Slf4j
@Service
public class SysOrgServiceImpl extends ServiceImpl<SysOrgDao, SysOrg> implements SysOrgService {
    @Autowired
    SysOrgDao sysOrgDao;

    public SysOrg getTreeById(Long id) {
        return sysOrgDao.getTreeById(id);
    }

    @Override
    public SysOrg getTreeByIdWithRole(Long id) {
        return sysOrgDao.getTreeByIdWithRole(id);
    }

    @Override
    public List<SysOrg> getChildrenTreeByParentId(Long parentId) {
        return sysOrgDao.getChildrenTreeByParentId(parentId);
    }

    @Override
    public List<SysOrg> getChildrenTreeByParentIdWithRole(Long parentId) {
        return sysOrgDao.getChildrenTreeByParentIdWithRole(parentId);
    }

    /**
     * @param id
     * @return
     * @author: ares
     * @date: 2021/9/30 21:06
     * @description: 根据子节点ID查询上级tree
     */
    @Override
    public SysOrg getTreeByIdAsLeaf(Long id) {
        return getParent(sysOrgDao.selectById(id));
    }


    /**
     * @param children
     * @return
     * @author: ares
     * @date: 2021/9/30 22:10
     * @description: 根据子节点查询父节点
     */
    private SysOrg getParent(SysOrg children) {
        if (children == null) return null;
        Long parentId = children.getParentId();
        if (parentId == 0) {
            return children;
        } else {
            SysOrg parent = sysOrgDao.selectById(parentId);
            parent.setChildren(Arrays.asList(children));
            return getParent(parent);
        }
    }

    /**
     * @param id
     * @return
     * @author: ares
     * @date: 2021/10/1 21:10
     * @description: 根据id删除自身及子节点
     */
    @Override
    public boolean removeSelfAndChildrenById(Long id) {
        List<SysOrg> children = sysOrgDao.getChildrenByParentId(id);
        if (children.size() > 0) {
            children.forEach(child -> removeSelfAndChildrenById(child.getId()));
        }
        return super.removeById(id);
    }

    @Override
    public List<SysOrg> getCurrentUserOrgs(Long userId) {
        return sysOrgDao.getCurrentUserOrgs(userId);
    }

    @Override
    public List<SysOrg> getCurrentUserOrgsWithRole(Long userId) {
        return sysOrgDao.getCurrentUserOrgsWithRole(userId);
    }

    @Override
    public List<SysOrg> getListByOrgEntity(SysOrg org) {
        QueryWrapper<SysOrg> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(org);
        return sysOrgDao.selectList(queryWrapper);
    }

    @Override
    public List<SysOrg> getChildrenByParentId(Long parentId) {
        return sysOrgDao.getChildrenByParentId(parentId);
    }

    @Override
    public List<SysOrg> getChildrenByParentIdWithRole(Long parentId) {
        return sysOrgDao.getChildrenByParentIdWithRole(parentId);
    }

    @Override
    public List<SysOrg> getListByUserIdWithRole(Long userId, String currentOrgType) {
        return sysOrgDao.getListByUserIdWithRole(userId, currentOrgType);
    }
}
